package torres.view;

import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

public class ImageLoader {
	 private BufferedImage img;
	 private ImageIcon imgIcon;
	 private String rootDirectory = "src/resources/images/";
	
	/**
	 * Creates a new ImageLoader from the specified file name.
	 * @param s The name of the file to be loaded.
	 */
	public ImageLoader(String s) {		 
		 try {
			 img = ImageIO.read(new File(rootDirectory + s));
		 } 
		 catch (IOException e) {
			 System.err.println("IL: Image not found: " + s);
			 e.printStackTrace();
		 }
		 setImgIcon(new ImageIcon(img));
	 }
	

	/**
	 * Returns the default size of the images.
	 * @return The default size of the images. 
	 */
	public Dimension getPreferredSize() {
        
    	if (img == null) {
    		return new Dimension(100,100);
    	} 
    	else {
    		return new Dimension(img.getWidth(null), img.getHeight(null));
    	}
    }

	/**
	 * Sets the image icon to the specified icon.
	 * @param imgIcon The new image icon.
	 */
	public void setImgIcon(ImageIcon imgIcon) {
		this.imgIcon = imgIcon;
	}
	
	public static BufferedImage loadImage(String name) {
		BufferedImage image = null;
		String rootDirectory = "src/resources/images/";
		
		try {
			 image = ImageIO.read(new File(rootDirectory + name));
		 } 
		 catch (IOException e) {
			 System.err.println("IL: Image not found: " + name);
			 e.printStackTrace();
		 }
		 
		 return image;
	}

	
	/**
	 * Returns the current image icon.
	 * @return The current image icon.
	 */
	public ImageIcon getImgIcon() {
		return imgIcon;
	}
	
	public BufferedImage getImage() {
		
		return img;
	}
}